package org.snail.serialization;


import io.netty.util.collection.ByteObjectHashMap;
import io.netty.util.collection.ByteObjectMap;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.snail.common.SnailClassLoader;

/**
 * 序列化工厂类
 *
 * @author fish
 */
public final class SerializerFactory {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(SerializerFactory.class);

    private static final ByteObjectMap<Serializer> serializers = new ByteObjectHashMap<>();

    static {
        //这里可以用类加载的方式加载其余序列化类型
        serializers.put((byte) 0x01, new Serializer());
        logger.info("Supported serializers: {}.", serializers);
    }

    public static Serializer getSerializer(byte code) {

        Serializer serializer = serializers.get(code);

        if (serializer == null) {
            throw new NullPointerException("unsupported serializerImpl with code: " + code);
        }
        return serializer;
    }


    public static Serializer getDefaultSerializer() {
        Serializer serializer = serializers.get((byte) 0x01);
        if (serializer == null) {
            throw new NullPointerException("unsupported serializerImpl with code: " + (byte) 0x01);
        }
        return serializer;
    }
}
